<?php
class DomArQueryTestCase extends DomArTestUnitTestCase { 
	
	public function testLoadMultiple() {
		
		DomArQueryTestClass::getTable()->update();
		
		$a = new DomArQueryTestClass();
		$a->name = 'a';
		$b = new DomArQueryTestClass();
		$b->name = 'b';
		
		$c1 = new DomArQueryChildTestClass();
		$c1->name  = 'c1';
		$c2 = new DomArQueryChildTestClass();
		$c2->name  = 'c2';
		$c3 = new DomArQueryChildTestClass();
		$c3->name  = 'c3';
		
		$a->child = $c1;
		$b->child = $c2;
		
		$a->save();
		$b->save();
		$c3->save();
		
		DomAr::flushStoredObjects();
		$this->assertIdentical( DomAr::getObject($a->id, 'DomArQueryTestClass')->id, null );
		
		// -----------------------------------------------
		// test using DomArQuery and build up from zero.
		$result = DomArQuery::c()
			->from( DomArQueryTestClass::getTableName(),'a')
			->leftJoin( DomArQueryChildTestClass::getTableName(), 'b', 'b.parent = a.id')
			->loadsMultipleObjects(true)
			->setMainObjectAlias('b')
			->commit()->toArray();
		// test that object class name is correct
		$this->assertEqual( get_class($result[0]), 'DomArQueryChildTestClass');
		
		// test that object exists in cache. If it does, then leftJoinProperty works.
		$this->assertTrue( $a->id > 0 );
		$this->assertIdentical( DomAr::getObject($a->id, 'DomArQueryTestClass')->id, $a->id );

		
		// run again to test that objects are loaded from memory correctly
		$result = DomArQuery::c()
			->from( DomArQueryTestClass::getTableName(),'a')
			->leftJoin( DomArQueryChildTestClass::getTableName(), 'b', 'b.parent = a.id')
			->loadsMultipleObjects(true)
			->setMainObjectAlias('b')
			->commit()->toArray();
		// test that object class name is correct
		$this->assertEqual( get_class($result[0]), 'DomArQueryChildTestClass');
		
		DomAr::flushStoredObjects();

		// -----------------------------------------------
		// test joinProperty using DomArQuery
		$result = DomArQuery::c()
			->from( DomArQueryTestClass::getTableName(),'a')
			->setClassName('DomArQueryTestClass')
			->joinProperty('child', 'b')
			->setMainObjectAlias('b')
			->commit()->toArray();

		// test that object class name is correct
		$this->assertEqual( get_class($result[0]), 'DomArQueryChildTestClass');
		
		// test that object exists in cache. If it does, then leftJoinProperty works.
		$this->assertTrue( $a->id > 0 );
		$this->assertIdentical( DomAr::getObject($a->id, 'DomArQueryTestClass')->id, $a->id );
		
		DomAr::flushStoredObjects();
		
		// -----------------------------------------------
		// test joinProperty
		$result = DomArQueryTestClass::query()
			->leftJoinProperty( 'child', 'b')
			->setMainObjectAlias('b')
			->commit()->toArray();
		
		// test that object class name is correct
		$this->assertEqual( get_class($result[0]), 'DomArQueryChildTestClass');
		
		// test that object exists in cache. If it does, then leftJoinProperty works.
		$this->assertTrue( $a->id > 0 );
		$this->assertIdentical( DomAr::getObject($a->id, 'DomArQueryTestClass')->id, $a->id );
		
		DomAr::flushStoredObjects();
		
		// -----------------------------------------------
		// test joinProperty vica versa
		$result = DomArQueryChildTestClass::query()
			->leftJoinProperty( 'parent', 'b')
			->setMainObjectAlias('b')
			->commit()->toArray();
		
		// test that object class name is correct
		$this->assertEqual( get_class($result[0]), 'DomArQueryTestClass');
		
		// test that object exists in cache. If it does, then leftJoinProperty works.
		$this->assertTrue( $a->id > 0 );
		$this->assertIdentical( DomAr::getObject($a->id, 'DomArQueryChildTestClass')->id, $a->id );
		
		DomAr::flushStoredObjects();
	}

	public function testQueryIndexColumnName() {
		
		DomArQueryTestClass::getTable()->update();
		
		$a = new DomArQueryTestClass();
		$a->name = 'a';
		$b = new DomArQueryTestClass();
		$b->name = 'b';
		
		$a->save();
		$b->save();
		
		DomAr::flushStoredObjects();

		$result = DomArQuery::c()->setIndexColumnName('id')->from( DomArQueryTestClass::getTableName(),'t')->commit()->toArray();

		$this->assertTrue( empty($result[0]) );
		$this->assertIdentical( $result[$a->id], $a );
		$this->assertIdentical( $result[$b->id], $b );
		
		DomAr::flushStoredObjects();
		
		$result = DomArQuery::c()->from( DomArQueryTestClass::getTableName(),'t')->commit()->toArray();
		$this->assertIdentical( $result[0], $a );
		$this->assertIdentical( $result[1], $b );
		
	}
	
	public function testQueryMultipleResults() {
		
		DomArQueryTestClass::getTable()->update();
		
		$a = new DomArQueryTestClass();
		$a->name = 'a';
		$b = new DomArQueryTestClass();
		$b->name = 'b';
		
		$c1 = new DomArQueryChildTestClass();
		$c1->name  = 'c1';
		$c2 = new DomArQueryChildTestClass();
		$c2->name  = 'c2';
		$c3 = new DomArQueryChildTestClass();
		$c3->name  = 'c3';
		
		$a->child = $c1;
		$b->child = $c2;
		
		$a->save();
		$b->save();
		$c3->save();
		
		DomAr::flushStoredObjects();
		$this->assertIdentical( DomAr::getObject($a->id, 'DomArQueryTestClass')->id, null );
		
		$result = DomArQuery::c()
			->from( DomArQueryTestClass::getTableName(),'a')
			->leftJoin( DomArQueryChildTestClass::getTableName(), 'b', 'b.parent = a.id')
			->loadsMultipleObjects(true)			
			->setMainObjectAlias(NULL)
			->commit()->toArray();
		
		// test that object class name is correct
		$this->assertEqual( get_class($result[0]['a']), 'DomArQueryTestClass');
		$this->assertEqual( get_class($result[0]['b']), 'DomArQueryChildTestClass');
		
		// test that object exists in cache. If it does, then leftJoinProperty works.
		$this->assertTrue( $a->id > 0 );
		$this->assertIdentical( DomAr::getObject($a->id, 'DomArQueryTestClass')->id, $a->id );
		
		
	}
}
?>